//
//  CTPickAlbumView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/16.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPickAlbumView.h"
#import "CTDebugManager.h"
#import "CTPickAlbumCell.h"
#import "CTHeader.h"


@interface CTPickAlbumView ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * albumTableView;
    NSArray * infoArray;
}
@end


@implementation CTPickAlbumView
@synthesize delegate;


- (void)dealloc
{
    [albumTableView removeFromSuperview];
    albumTableView = nil;
    
    infoArray = nil;
    delegate = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPickAlbumView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0,-CT_HEADER_SCREEN_HEIGHT+200, CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT-200 );
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        [self creatTableView];
    }
    return self;
}


-(void)creatTableView
{
    UIView * shadowView = [[UIView alloc] initWithFrame:self.bounds];
    shadowView.backgroundColor = [UIColor whiteColor];
    shadowView.alpha = 0.8;
    [self addSubview:shadowView];
    
    albumTableView  = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    albumTableView .delegate = self;
    albumTableView .dataSource = self;
    albumTableView .rowHeight = 120.0;
    albumTableView .showsVerticalScrollIndicator = NO;
    albumTableView .separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    albumTableView .autoresizingMask = UIViewAutoresizingFlexibleWidth;
    albumTableView .backgroundColor = [UIColor clearColor];
    [albumTableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:albumTableView];
    

    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [shadowView addGestureRecognizer:singleRecognizer];
}



#pragma mark - tableView delegate -



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [infoArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * identifier = @"cell";
    CTPickAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
       cell = [[CTPickAlbumCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
    }
   
    CTAlbumItem * item = (CTAlbumItem *)[infoArray objectAtIndex:indexPath.row];
    cell.albumItem = item;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTAlbumItem * item = (CTAlbumItem *)[infoArray objectAtIndex:indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(albumPickedAction:withItem:)]) {
        [self.delegate albumPickedAction:self withItem:item];
    }
}

#pragma mark - action -

-(void)handleSingleTapFrom
{
    if ([self.delegate respondsToSelector:@selector(albumPickViewHiddenAction:)]) {
        [self.delegate albumPickViewHiddenAction:self];
    }
}

#pragma mark - setter -

-(void)setAlbumArray:(NSArray *)albumArray
{
    infoArray = albumArray;
    [albumTableView reloadData];
}



@end
